home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12681 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: pegasus.odyssee.net!news
  2. From: imaze@odyssee.net (Marc Mazerolle)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ problem with constructor.
  5. Date: Thu, 21 Mar 1996 00:17:44 GMT
  6. Organization: Odyssee Internet
  7. Message-ID: <4iq74v$enb@pegasus.odyssee.net>
  8. References: <DoGGGp.Muy@latcs1.lat.oz.au>
  9. Reply-To: imaze@odyssee.net
  10. NNTP-Posting-Host: pool15_12.odyssee.net
  11. X-Newsreader: Forte Free Agent v0.55
  12.  
  13. boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles) wrote:
  14.  
  15. >See astericks
  16.  
  17. >// main.cpp
  18.  
  19. >#include <iostream.h>
  20. >#include "defines.h"
  21. >#include "address.h"
  22.  
  23. >int main()
  24. >{
  25. >     Address Address1(56,"Derby Drive","Epping",3165);
  26. >     Address Address2(22,"Claremont Street","Fawkner",3060);
  27.  
  28. >     /********************************************************
  29. >      After the above two calls Address1 and Address2 contain
  30. >      all the appropriate data however after the next call all
  31. >      3 contain 0 or null strings. WHY?
  32. >      ********************************************************/
  33.  
  34. >     Address Address3(Address1);
  35. >     return(0);
  36. >}
  37.  
  38.  
  39.  
  40. >// address.cpp
  41.  
  42. >#include "address.h"
  43. >#include <string.h>
  44.  
  45. >// Constructors
  46. >Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
  47. >{
  48. >     Number=ANumber;
  49. >     strcpy(Street,AStreet);
  50. >     strcpy(City,ACity);
  51. >     Zip=AZip;
  52. >}
  53.  
  54. >Address::Address()
  55. >{
  56. >     Number=0;
  57. >     strcpy(Street,"");
  58. >     strcpy(City,"");
  59. >     Zip=0;
  60. >}
  61.  
  62. >// Copy constructor
  63. >Address::Address(Address& AnAddress)
  64. >{
  65. >     Number=AnAddress.Number;
  66. >     strcpy(Street,AnAddress.Street);
  67. >     strcpy(City,AnAddress.City);
  68. >     Zip=AnAddress.Zip;
  69. >}
  70.  
  71. >// Deconstructor
  72. >Address::~Address()
  73. >{
  74. >     Number=0;
  75. >     strcpy(Street,"");
  76. >     strcpy(City,"");
  77. >     Zip=0;
  78. >}
  79.  
  80.  
  81. >// address.h
  82.  
  83. >#ifndef __ADDRESS_H
  84. >#define __ADDRESS_H
  85. >#define STR_STREET " street"
  86.  
  87. >#include "defines.h"
  88.  
  89. >class Address
  90. >{
  91. >     private : int Number;
  92. >           char *Street;
  93. >           char *City;
  94. >           int Zip;
  95.  
  96. >     public : // Constructors
  97. >          Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
  98. >          Address();
  99. >          // Copy constructor
  100. >          Address(Address& AnAddress);
  101. >          // Deconstructor
  102. >          ~Address();
  103. >};
  104.  
  105. >#endif
  106.  
  107. When you declare "char *Street" in your class, you only declare a
  108. pointer, no space is reserved for the content of a string. Then you
  109. string-copy to this pointer (your lucky it does not bomb right there
  110. at your first copy). Before copying, you should allocate memory to the
  111. pointer like this :
  112.  
  113. // Add 1 byte for the null at the end of string
  114. Street = new char[strlen(AStreet) + 1]  ;
  115. // Then copy the string
  116. strcpy(Street,AStreet);
  117.  
  118. In your destructor (not deconstructor) don't forget to free the memory
  119. allocated by "new" by doing :
  120.  
  121. delete [] Street;
  122.  
  123. Otherwise, after some times, you will run out of memory....
  124.  
  125.